package com.silence.gmall.web.controller;

import com.silence.gmall.base.Result;
import com.silence.gmall.web.feign.ItemFeignClient;
import com.silence.gmall.web.vo.res.SkuDetailResVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Objects;

/**
 * @Author silence
 * @Date 2025-08-26 10:42
 * @Email 792245022@qq.com
 * @Version 1.0
 * @Description
 */
@Controller
@RequiredArgsConstructor
public class ItemController {

    private final ItemFeignClient itemFeignClient;

    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model) {
        Result<SkuDetailResVo> result = itemFeignClient.getSkuDetail(skuId);

        if (result.checkSuccess()) {
            SkuDetailResVo data = result.getData();

            if (Objects.isNull(data)) {
                return "item/error";
            }

            model.addAttribute("categoryView", data.getCategoryView());
            model.addAttribute("skuInfo", data.getSkuInfo());
            model.addAttribute("price", data.getPrice());
            model.addAttribute("spuSaleAttrList", data.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson", data.getValuesSkuJson());
        }

        return "item/index";
    }

}
